package com.custom.jdbc.handler;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author  Xiao-Bai
 * @since  2022/11/13 1:14
 * 
 */
public class CharacterTypeHandler extends AbstractTypeHandler<Character> {
    @Override
    public Character getTypeValue(Object val) {
        if (val == null) {
            return null;
        }
        else if (val instanceof Character) {
            return (Character) val;
        }
        else if (val instanceof Boolean) {
            return (char) ((boolean) val ? 1 : 0);
        }
        return null;
    }

    @Override
    public Character getTypeValue(ResultSet rs, int index) throws SQLException {
        String res = rs.getString(index);
        if (res != null && !res.isEmpty()) {
            return res.charAt(0);
        }
        return null;
    }

    @Override
    public void setTypeValue(PreparedStatement statement, int index, Character val) throws SQLException {
        statement.setString(index, val.toString());
    }
}
